﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Windows.Forms;
using System.Drawing;
using System.Data.SqlClient;

namespace Helper_Excel
{
    /// <summary>
    /// 此类负责导入Excel时将数据展示到界面
    /// </summary>
    public static class Import_Service
    {
        static Form_Main mainForm;
        /// <summary>
        /// 将DataSet数据源填充到界面
        /// </summary>
        /// <param name="_mainForm"></param>
        /// <param name="ds"></param>
        public static void DoInput(Form_Main _mainForm, DataSet ds)
        {
            GC.Collect();
            mainForm = _mainForm;
            try
            {
                //清除TableControl的Pages
                mainForm.tabControl1.Controls.Clear();
                for (int i = 0; i < ds.Tables.Count; i++)
                {
                    //为TableControl添加一个Page
                    mainForm.tabControl1.TabPages.Add("page" + (i + 1).ToString(), (i + 1).ToString() + "·" + ds.Tables[i].TableName);
                    //实例化一个DataGridView
                    DataGridView dgv = new DataGridView();
                    //添加事件，使此DataGridView自动绘制行号
                    dgv.RowPostPaint += new DataGridViewRowPostPaintEventHandler(dgvExcel_RowPostPaint);
                    dgv.Name = "dgv" + (i + 1).ToString();
                    dgv.Dock = DockStyle.Fill;
                    dgv.ReadOnly = true;
                    dgv.AllowUserToAddRows = false;
                    dgv.Columns.Clear();
                    DataTable dt = ds.Tables[i];
                    //为dgv添加列
                    foreach (DataColumn dc in dt.Columns)
                    {
                        DataGridViewTextBoxColumn dgvc = new DataGridViewTextBoxColumn();
                        dgvc.HeaderText = dc.ColumnName;
                        dgvc.DataPropertyName = dc.ColumnName;
                        dgv.Columns.Add(dgvc);
                    }
                    dgv.DataSource = dt;
                    //将此DataGridView填充到Page
                    mainForm.tabControl1.TabPages[i].Controls.Add(dgv);
                }
            }
            catch (Exception ex)
            {
                IfError(ex);
            }
        }

        /// <summary>
        /// 绘制DataGridView行号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void dgvExcel_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X, Convert.ToInt32(e.RowBounds.Location.Y + (e.RowBounds.Height - (sender as DataGridView).RowHeadersDefaultCellStyle.Font.Size) / 2), (sender as DataGridView).RowHeadersWidth - 4, e.RowBounds.Height);
            TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), (sender as DataGridView).RowHeadersDefaultCellStyle.Font, rectangle, (sender as DataGridView).RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.Right);
        }
        /// <summary>
        /// 也许、如果、万一、可能一不小心悲剧了，给点反应
        /// </summary>
        /// <param name="ex"></param>
        private static void IfError(Exception ex)
        {
            //清除TableControl的Pages
            mainForm.tabControl1.Controls.Clear();
            //实例化一个文本框
            TextBox txt = new TextBox();
            txt.ForeColor = Color.Red;
            txt.Multiline = true;
            //在文本框中显示一些错误信息
            txt.Text = "错误信息：" + ex.Message;
            txt.Dock = DockStyle.Fill;
            txt.ReadOnly = true;
            //在TabControl中添加一个page
            mainForm.tabControl1.TabPages.Add("导入失败");
            //在page中添加用于显示错误信息的TextBox
            mainForm.tabControl1.TabPages[0].Controls.Add(txt);
        }
    }
}
